package com.ruoyi.web.controller.system;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysCourse;
import com.ruoyi.system.domain.SysStudent;
import com.ruoyi.system.mapper.SysStudentMapper;
import com.ruoyi.system.service.ISysStudentService;

/**
 * 学员名册信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/student")
public class SysStudentController extends BaseController
{
    @Autowired
    private ISysStudentService sysStudentService;
    @Autowired
    private SysStudentMapper sysStudentMapper;

    /**
     * 获取学员列表
     */
    @PreAuthorize("@ss.hasPermi('system:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysStudent entity, Integer pageNum, Integer pageSize)
    {
        List<SysStudent> list = sysStudentService.list(entity, pageNum, pageSize);
        Integer total = sysStudentMapper.count(entity);
        return getDataTable(list, total.longValue());
    }

    /**
     * 根据学员编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:student:query')")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId)
    {
        return success(sysStudentService.selectById(studentId));
    }

    /**
     * 新增学员
     */
    @PreAuthorize("@ss.hasPermi('system:student:add')")
    @Log(title = "学员管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysStudent entity)
    {
        // 密码加密
        entity.setPassword(SecurityUtils.encryptPassword(entity.getPassword()));
        return toAjax(sysStudentService.insert(entity));
    }

    /**
     * 修改学员
     */
    @PreAuthorize("@ss.hasPermi('system:student:edit')")
    @Log(title = "学员管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysStudent student)
    {
        return toAjax(sysStudentService.update(student));
    }

    /**
     * 删除学员
     */
    @PreAuthorize("@ss.hasPermi('system:student:remove')")
    @Log(title = "学员管理", businessType = BusinessType.GRANT)
    @DeleteMapping("/remove/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds)
    {
        return toAjax(sysStudentService.deleteByIds(studentIds));
    }

    /**
     * 更新学习设置
     */
    @PreAuthorize("@ss.hasPermi('system:student:edit')")
    @Log(title = "学员管理", businessType = BusinessType.UPDATE)
    @PutMapping("/settings")
    public AjaxResult settings(@RequestBody SysStudent entity)
    {
        // 这里需要实现学习设置的更新逻辑
        // 由于目前没有对应的字段，可以先返回成功
        return toAjax(sysStudentService.update(entity));
    }

    /**
     * 批量添加学员-课程
     */
    @PreAuthorize("@ss.hasPermi('system:student:edit')")
    @Log(title = "学员名册", businessType = BusinessType.GRANT)
    @PostMapping("/addStudentCourseBatch/{studentIds}/{courseIds}")
    public AjaxResult addStudentCourseBatch(@PathVariable Long[] studentIds, @PathVariable Long[] courseIds)
    {
    	for(Long studentId: studentIds) {
    		sysStudentService.insertStudentCourse(studentId, courseIds);
    	}
        return AjaxResult.success();
    }

    /**
     * 获取册本树
     */
    @PreAuthorize("@ss.hasPermi('system:student:list')")
    @GetMapping("/courseTree")
    public AjaxResult courseTree(SysCourse sysCourse)
    {
        return success(sysStudentService.courseTree(sysCourse));
    }
}